﻿using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using Web_Oto.Business;
using Web_Oto.Common;
using Web_Oto.Data.Dto;

namespace Administration
{
    public partial class Category : Page
    {
        private CategoryBus _business;

        protected void Page_Load(object sender, EventArgs e)
        {
            _business = new CategoryBus();

            if (!Page.IsPostBack)
            {
                hdfActiveSearch.Value = "0";

                var categories = _business.GetCategoryParents();
                categories.Insert(0, new Web_Oto.Data.Category { Id = 0, Name = " --- CHỌN DANH MỤC --- " });
                rcbParentCategory.DataSource = categories;
                rcbParentCategory.DataValueField = "Id";
                rcbParentCategory.DataTextField = "Name";
                rcbParentCategory.DataBind();
            }

            msgBox.InnerHtml = string.Empty;
            msgBox.Attributes["class"] = "none;";
        }

        protected void gridCategory_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            var categoryId = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Id"].ToString();
            _business = new CategoryBus();
            var id = 0;
            int.TryParse(categoryId, out id );
            var result = _business.Delete(id);
            if (result > 0)
            {
                msgBox.InnerHtml = "<p><strong>Xóa thành công</strong></p>";
                msgBox.Attributes["class"] = "msg msg-ok";
            }
            else
            {
                msgBox.InnerHtml = "<p><strong>Xóa không thành công</strong></p>";
                msgBox.Attributes["class"] = "msg msg-error";
            }
        }

        protected void gridCategory_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            if (hdfActiveSearch.Value == "1")
            {
                var strParentId = rcbParentCategory.SelectedValue;
                int parentId;
                int.TryParse(strParentId, out parentId);
                gridCategory.DataSource = _business.GetCategoryDtos(txtName.Text, parentId);
            }
            else
                gridCategory.DataSource = _business.GetCategoryDtos();
        }

        protected void gridCategory_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode))
            {
                var edititem = (GridEditableItem)e.Item;
                var values = new Hashtable();
                edititem.ExtractValues(values);
                
                //Load data for drop list box.
                var ddlParent = ((RadComboBox)edititem.FindControl("ddlParent"));
                var categories = _business.GetCategoryParents();
                categories.Insert(0, new Web_Oto.Data.Category {Id= 0, Name = " --- CHỌN DANH MỤC --- "});
                ddlParent.DataSource = categories;
                ddlParent.DataValueField = "Id";
                ddlParent.DataTextField = "Name";
                ddlParent.DataBind();

                var parentId = ((HiddenField) edititem.FindControl("parentId")).Value;
                ddlParent.SelectedValue = string.IsNullOrEmpty(parentId) ? "0" : parentId;
            }
        }

        protected void gridCategory_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            var gdItem = (e.Item as GridEditFormItem);
            var editableItem = ((GridEditableItem)e.Item);
            var values = new Hashtable();
            editableItem.ExtractValues(values);
            
            var category = new Web_Oto.Data.Category
            {
                Id = (int)editableItem.GetDataKeyValue("Id")                
            };            

            if (gdItem != null)
            {
                var ddlParent = gdItem.FindControl("ddlParent") as RadComboBox;
                var txtName = gdItem.FindControl("txtName") as RadTextBox;
                var txtSEO_Title = gdItem.FindControl("txtSEO_Title") as RadTextBox;
                var txtSEO_Keyword = gdItem.FindControl("txtSEO_Keyword") as RadTextBox;
                var txtSEO_Description = gdItem.FindControl("txtSEO_Description") as RadTextBox;
                if (ddlParent != null && txtName != null && txtSEO_Title != null && 
                    txtSEO_Keyword != null && txtSEO_Description != null)
                {                    
                    var strParentId = ddlParent.SelectedValue;
                    int parentId;
                    int.TryParse(strParentId, out parentId);
                    category.ParentId = parentId;
                    category.Name = txtName.Text;
                    category.SEO_Title = txtSEO_Title.Text;
                    category.SEO_Keyword = txtSEO_Keyword.Text;
                    category.SEO_Description = txtSEO_Description.Text;

                    if (!CheckValidData(category.Name))
                        return;

                    var result = _business.Update(category);
                    if (result > 0)
                    {
                        msgBox.InnerHtml = "<p><strong>Cập nhật thành công</strong></p>";
                        msgBox.Attributes["class"] = "msg msg-ok";
                    }
                    else
                    {
                        msgBox.InnerHtml = "<p><strong>Cập nhật không thành công</strong></p>";
                        msgBox.Attributes["class"] = "msg msg-error";
                    }
                }
            }
        }

        protected void gridCategory_InsertCommand(object sender, GridCommandEventArgs e)
        {
            var gdItem = (e.Item as GridEditFormItem);
            var editableItem = ((GridEditableItem)e.Item);
            var values = new Hashtable();
            editableItem.ExtractValues(values);

            var category = new Web_Oto.Data.Category
            {                
                Name = (string)values["Name"],
                SEO_Title = (string)values["SEO_Title"],
                SEO_Keyword = (string)values["SEO_Keyword"],
                SEO_Description = (string)values["SEO_Description"]
            };

            if (gdItem != null)
            {
                var ddlParent = gdItem.FindControl("ddlParent") as RadComboBox;
                var txtName = gdItem.FindControl("txtName") as RadTextBox;
                var txtSEO_Title = gdItem.FindControl("txtSEO_Title") as RadTextBox;
                var txtSEO_Keyword = gdItem.FindControl("txtSEO_Keyword") as RadTextBox;
                var txtSEO_Description = gdItem.FindControl("txtSEO_Description") as RadTextBox;
                if (ddlParent != null && txtName != null && txtSEO_Title != null &&
                    txtSEO_Keyword != null && txtSEO_Description != null)
                {
                    var strParentId = ddlParent.SelectedValue;
                    int parentId;
                    int.TryParse(strParentId, out parentId);
                    category.ParentId = parentId;
                    category.Name = txtName.Text;
                    category.SEO_Title = txtSEO_Title.Text;
                    category.SEO_Keyword = txtSEO_Keyword.Text;
                    category.SEO_Description = txtSEO_Description.Text;

                    if (!CheckValidData(category.Name))
                        return;

                    var result = _business.Insert(category);
                    if (result > 0)
                    {
                        msgBox.InnerHtml = "<p><strong>Thêm mới thành công</strong></p>";
                        msgBox.Attributes["class"] = "msg msg-ok";
                    }
                    else
                    {
                        msgBox.InnerHtml = "<p><strong>Thêm mới không thành công</strong></p>";
                        msgBox.Attributes["class"] = "msg msg-error";
                    }
                }
            }
        }

        private bool CheckValidData(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                msgBox.InnerHtml = "<p><strong>Vui lòng nhập tên loại xe</strong></p>";
                msgBox.Attributes["class"] = "msg msg-error";

                return false;
            }

            return true;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            //Keep show status of box search.
            hdfActiveSearch.Value = "1";

            //Search category by name and category.
            var strParentId = rcbParentCategory.SelectedValue;
            int parentId;
            int.TryParse(strParentId, out parentId);
            gridCategory.DataSource = _business.GetCategoryDtos(txtName.Text, parentId);
            gridCategory.DataBind();
        }

    }
}